/** 
 * 编辑程序手册
*/
<template>
    <vp-dialog ref="dialogRef">    
        <template #title>
            <vp-label v-if="type == 'add'" zh="新增" en="Add" align="left"/>
            <vp-label v-else zh="修订" en="Revision" align="left"/>
        </template>
        <QualityManual ref="manualRef" :id="id" :type="type"></QualityManual>
        <template #footer>
            <vp-btn-save  :loading="saveLoading" @click="handleSave(10)"></vp-btn-save>
            <VpProcessToolBar 
                :loading="saveLoading" 
                :set-loading-fn="(_loading: boolean) => { saveLoading = _loading }"
                :before-show-fn="beforeShowFn" 
                :after-agree-fn="afterAgreeFn"
            />    
        </template>
    </vp-dialog>
</template>

<script setup lang="ts" name="EditQualityManual">
import { defineAsyncComponent, defineExpose, ref } from 'vue'
import request from '/@/utils/request';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';
const QualityManual = defineAsyncComponent(() => import('./QualityManual.vue'));

const emits = defineEmits<{
    (e: 'formSubmited'): void
}>()

const dialogRef = ref();
const type = ref('add');
const id = ref('');
const saveLoading = ref(false);

const manualRef = ref();

const handleOpen = (_id: string, _type: string)=>{
    id.value = _id;
    type.value = _type;
    dialogRef.value.handleOpen();
}

const handleSave = (zt: number) =>{
    saveLoading.value = true;
    manualRef.value.getData(zt).then((data: any) =>{
        request.post("/quality/manual/" + type.value, {
            ...data,
            zt: zt,
        }).then(() => {
            emits('formSubmited')
            dialogRef.value.handleClose();
        })
        .catch(() => {})
        .finally(() => {
            saveLoading.value = false;
        });
    }).catch(()=>{saveLoading.value = false;});    
}


const beforeShowFn = () =>{
    saveLoading.value = true;
    return manualRef.value.getData(88).finally(()=>{saveLoading.value = false;});
}

const afterAgreeFn = (processHandle:any) =>{
    saveLoading.value = true;
    manualRef.value.getData(88).then((data: any) =>{
        request.post("/quality/manual/" + type.value, {
            ...data,
            zt: 88,
            processHandle: processHandle,
        }).then(()=>{
            emits('formSubmited')
            dialogRef.value.handleClose();
        }).catch(()=>{
        }).finally(()=>{saveLoading.value = false;});
    }).catch(()=>{saveLoading.value = false;});
}

defineExpose({
    handleOpen
})
</script>
<style scoped lang="scss">

</style>
